package com.expect.admin.busmanagement.data.dao;

import java.util.List;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.expect.admin.busmanagement.data.dataobject.Bus;

public interface BusRepository extends JpaRepository<Bus, String> {
	
	public Bus findById(String id);
	
	/**
	 * 车辆编号
	 */
	public Bus findByBh(String busBh);
	
	/**
	 * 驾驶员
	 */
	public Bus findByUserName(String userName);//用String还是User？
	
	public List<Bus> findAll(Specification<Bus> spec);
	
//	public List<Bus> findByApplicationClassifyAndApplicationStatement(String applicationClassify,String applicationStatement);
	
	List<Bus> findByApplicationStatementOrderByApplicationDateDesc(String condition);

	/**
	 * 某用户已回填的记录
	 * @param userId
	 * @return
	 */
	@Query("select distinct b from Bus b, Lcrzb l where b.id = l.clnrid and l.user.id = ?1 and l.dyjd = 'T' order by b.applicationDate desc")
	List<Bus> findYhtApplication(String userId);
	
	@Query("select b from Bus b where b.userName.id = ?1 and b.applicationStatement <> 'T' and b.applicationStatement <> 'Y' and b.applicationStatement <> ?2 order by b.applicationDate desc")
	public List<Bus> findApplicationRecordWspList(String userId,String condition);
	
	@Query("select distinct b from Bus b, Lcrzb l where b.id = l.clnrid and l.user.id = ?1 and b.applicationStatement = ?2 order by b.applicationDate desc")
	public List<Bus> findByUserAndCondition(String userId,String condition);
	
	/**
	 * 已退回
	 * @param userId
	 * @param curCondition
	 * @return
	 */
	@Query("select distinct b from Bus b, Lcrzb l where b.applicationStatement =  ?2 and b.isReturned = 'Y' and b.id = l.clnrid " + "and l.user.id = ?1 order by b.applicationDate desc")
	public List<Bus> findYthApplication(String userId,String curCondition);
	
	@Query("select distinct b from Bus b, Lcrzb l where b.id = l.clnrid and l.user.id = ?1 and b.applicationStatement <> ?2 order by b.applicationDate desc")
	List<Bus> findYspApplication(String userId,String condition);
	
	/**
	 * 获取某用户未提交的申请记录
	 * @param userId
	 * @param condition
	 * @return
	 */
	List<Bus> findByUserName_idAndApplicationStatementOrderByApplicationDateDesc(String userId,String condition);

	@Query("update Bus b set b.bh = ?2 where b.id = ?1")
	public void updateBusBh(String id,String busBh);
}
